
#include <stdio.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "esp_system.h"
#include "esp_spi_flash.h"
#include "nvs_flash.h"

#include "lvgl.h"
#include "bsp_board.h"         //1.方案板参数配置初始化必须包含的头文件
#include "lvgl_wifi_connect.h" //2.方案板LVGL_wifi接口初始化必须包含的头文件
#include "lv_demos.h"
#include <string.h>
#include <sys/param.h>

#include "esp_wifi.h"
#include "esp_event.h"
#include "esp_http_client.h"
#include "gui_app/ui.h"
static const char *TAG = "app_main";
EventGroupHandle_t tcp_event_group; // wifi建立成功信号量
extern lv_obj_t *ui_Panel1;
extern lv_obj_t *ui_TextArea1;
lv_obj_t *ui_wifi_ground[30];
lv_obj_t *ui_wifi_ssid[30];
lv_obj_t *ui_wifi_rssi[30];

void ui_event_WifiTable(lv_event_t *e) // 点击wifi名称回调
{
    lv_event_code_t event_code = lv_event_get_code(e);
    lv_obj_t *target = lv_event_get_target(e);
    if (event_code == LV_EVENT_CLICKED)
    {
        _ui_label_set_property(ui_Label3, _UI_LABEL_PROPERTY_TEXT, lv_label_get_text(ui_wifi_ssid[(int)e->user_data]));
    }
    if (event_code == LV_EVENT_CLICKED)
    {
        _ui_screen_change(&ui_Screen2, LV_SCR_LOAD_ANIM_MOVE_LEFT, 200, 0, &ui_Screen2_screen_init);
    }
}

void wifi_scan() // 扫描可用wifi
{

    wifi_scan_init();
    // 延迟2秒,等待扫描结束
    vTaskDelay(2000 / portTICK_PERIOD_MS);
    // 存储可用 Wi-Fi 热点数量
    uint16_t ap_num;

    // 获取可用 Wi-Fi 热点数量
    esp_wifi_scan_get_ap_num(&ap_num);
    if (ap_num == 0)
    {
        // 如果没有可用 Wi-Fi 热点，输出提示信息
        ESP_LOGI(TAG, "No AP found");
        return;
    }
    // 存储 Wi-Fi 热点信息的指针
    wifi_ap_record_t *ap_records;
    // 分配内存空间存储 Wi-Fi 热点信息
    ap_records = (wifi_ap_record_t *)malloc(sizeof(wifi_ap_record_t) * ap_num);
    if (!ap_records)
    {
        ESP_LOGE(TAG, "malloc error");
        return;
    }
    // 获取所有可用 Wi-Fi 热点信息
    ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&ap_num, ap_records));

    // 输出可用 Wi-Fi 热点数量
    ESP_LOGI(TAG, "Found %d APs", ap_num);

    // 遍历所有可用 Wi-Fi 热点，并输出信息
    for (int i = 0; i < ap_num; i++)
    {
        ui_wifi_ground[i] = lv_obj_create(ui_Panel1);
        lv_obj_set_width(ui_wifi_ground[i], 480);
        lv_obj_set_height(ui_wifi_ground[i], 20);
        lv_obj_set_x(ui_wifi_ground[i], 0);
        lv_obj_set_y(ui_wifi_ground[i], -80 + i * 25);
        lv_obj_set_align(ui_wifi_ground[i], LV_ALIGN_CENTER);
        lv_obj_clear_flag(ui_wifi_ground[i], LV_OBJ_FLAG_SCROLLABLE); /// Flags
        lv_obj_add_event_cb(ui_wifi_ground[i], ui_event_WifiTable, LV_EVENT_ALL, i);

        ui_wifi_ssid[i] = lv_label_create(ui_wifi_ground[i]);
        lv_obj_set_width(ui_wifi_ssid[i], LV_SIZE_CONTENT);
        lv_obj_set_height(ui_wifi_ssid[i], LV_SIZE_CONTENT);
        lv_obj_set_x(ui_wifi_ssid[i], 0);
        lv_obj_set_y(ui_wifi_ssid[i], 0);
        lv_obj_set_align(ui_wifi_ssid[i], LV_ALIGN_CENTER);
        lv_label_set_text(ui_wifi_ssid[i], (const char *)ap_records[i].ssid);

        // ESP_LOGI(TAG, "SSID:%s\t RSSI:%d\t BSSID:" MACSTR, ap_records[i].ssid, ap_records[i].rssi, MAC2STR(ap_records[i].bssid));
    }
    // 释放内存
    free(ap_records);
}

void wifi_set_ok() // 设置wifi
{
    esp_wifi_disconnect();
    char *a = lv_label_get_text(ui_Label3);
    char *b = lv_textarea_get_text(ui_TextArea1);
    uint8_t c[strlen(a)];
    uint8_t d[strlen(b)];
    for (uint8_t i = 0; *(a + i) != '\0'; i++)
    {
        c[i] = *(a + i);
    }
    for (uint8_t i = 0; *(b + i) != '\0'; i++)
    {
        d[i] = *(b + i);
    }

    wifi_config_t wifi_config = {
        .sta = {
            .threshold.authmode = WIFI_AUTH_WPA2_PSK,
            .sae_pwe_h2e = WPA3_SAE_PWE_BOTH,
        },
    };

    memcpy(wifi_config.sta.ssid, c, sizeof c);
    memcpy(wifi_config.sta.password, d, sizeof d);

    // 配置
    ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
    ESP_ERROR_CHECK(esp_wifi_connect()); // 连接网络
    //   _ui_screen_change( &ui_Screen1, LV_SCR_LOAD_ANIM_MOVE_LEFT, 200, 0, &ui_Screen1_screen_init);
}

void app_main(void)
{
      tcp_event_group = xEventGroupCreate(); // 创建wifi事件组
    ESP_ERROR_CHECK(nvs_flash_init()); // nvs存储初始化

    // 1.方案板参数配置初始化
    sys_int();
    // 2.方案板LVGL wifi接口初始化
    lvgl_init_wifi();
    // 3.LVGL应用层,用户程序
    lv_port_sem_take1();
    ui_init();
    lv_port_sem_give1();
}
